﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApp
{
    internal class DemoSerialize
    {
        // 序列号与反序列化
        public void serialize()
        {
            //要将p这个对象 传输给对方电脑
            Person p = new Person();
            p.Name = "张三";
            using (FileStream fsWrite = new FileStream(@"C:\Users\SpringRain\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                //开始序列化对象
                //BinaryFormatter bf = new BinaryFormatter();
                //bf.Serialize(fsWrite, p);//已弃用
            }
            Console.WriteLine("序列化成功");
            Console.ReadKey();

            //接收对方发送过来的二进制 反序列化成对象
            using (FileStream fsRead = new FileStream(@"C:\Users\SpringRain\Desktop\111.txt", FileMode.OpenOrCreate, FileAccess.Read))
            {
                //BinaryFormatter bf = new BinaryFormatter();
                //p = (Person)bf.Deserialize(fsRead);//已弃用
            }
            Console.WriteLine(p.Name);
            Console.ReadKey();
        }

        public class Person
        {
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        }
    }
}
